package com.kbbywyclient.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.kbbywyclient.R;
import com.kbbywyclient.data.entity.LoanIndex;
import com.kbbywyclient.di.AppComponent;
import com.kbbywyclient.di.module.MineLoanModule;
import com.kbbywyclient.presenter.MineLoanPresenter;
import com.kbbywyclient.ui.activity.base.BaseActivity;
import com.kbbywyclient.ui.contarct.MineLoanContract;
import com.kbbywyclient.util.ActivitySkipUtil;
import com.kbbywyclient.util.StringUtil;
import com.kbbywyclient.util.pre.PreUtils;
import com.kbbywyclient.widget.CustomDialog;
import com.kbbywyclient.widget.CustomToolBar;

import java.text.DecimalFormat;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Jiangzx on 2017/9/6 13:37.
 * 我的贷款二级页面
 */

public class MineLoanActivity extends BaseActivity implements MineLoanContract.View{
    @BindView(R.id.v_toolbar)
    CustomToolBar toolBar;
    @BindView(R.id.tv_subscript_grab_order)
    TextView tvSubGrabOrder;
    @BindView(R.id.tv_subscript_exclusive_order)
    TextView tvSubExclusiveOrder;
    @BindView(R.id.tv_subscript_mine_order)
    TextView tvSubMineOrder;

    @Inject
    MineLoanPresenter presenter;

    private LoanIndex loanIndex;

    private CustomDialog dialog;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_mine_loan;
    }

    @Override
    protected void initView() {
        toolBar.setLeftViewOnClickLinster(v->finish());
    }

    @Override
    public void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        setScreenName("我的贷款");
        presenter.getLoanIndex(MineLoanPresenter.POSITION_NONE,
                String.valueOf(PreUtils.getUser(this).getSalesID()));
    }

    @Override
    public void initAppComponent(AppComponent appComponent) {
        appComponent.getMineLoanComponent(new MineLoanModule(this)).inject(this);
    }

    @OnClick({R.id.ll_grab_order,R.id.ll_exclusive_order,R.id.ll_mine_order,
            R.id.ll_mine_integral,R.id.ll_earn_integral,
            R.id.ll_store_manage,R.id.ll_product_manage,R.id.ll_business_consultant})
    public void onClick(View view){
        Activity activity=MineLoanActivity.this;
        switch (view.getId()){
            case R.id.ll_grab_order:
                //实时抢单
                if(loanIndex==null){
                    presenter.getLoanIndex(MineLoanPresenter.POSITION_GRAB_ORDER,
                            String.valueOf(PreUtils.getUser(this).getSalesID()));
                }else{
                    handleUserStatus(MineLoanPresenter.POSITION_GRAB_ORDER);
                }
                break;
            case R.id.ll_exclusive_order:
                //专属订单
                if(loanIndex==null){
                    presenter.getLoanIndex(MineLoanPresenter.POSITION_EXCLUSIVE_ORDER,
                            String.valueOf(PreUtils.getUser(this).getSalesID()));
                }else{
                    handleUserStatus(MineLoanPresenter.POSITION_EXCLUSIVE_ORDER);
                }
                break;
            case R.id.ll_mine_order:
                //我的订单
                if(loanIndex==null){
                    presenter.getLoanIndex(MineLoanPresenter.POSITION_MINE_ORDER,
                            String.valueOf(PreUtils.getUser(this).getSalesID()));
                }else{
                    handleUserStatus(MineLoanPresenter.POSITION_MINE_ORDER);
                }
                break;
            case R.id.ll_mine_integral:
                //我的积分
                ActivitySkipUtil.skipAnotherActivity(activity, MineScoreActivity.class);
                break;
            case R.id.ll_earn_integral:
                //推荐赚积分
                ActivitySkipUtil.skipAnotherActivity(activity, RecommendScoreActivity.class);
                break;
            case R.id.ll_store_manage:
                //店铺管理
                ActivitySkipUtil.skipAnotherActivity(activity, StoreSettingActivity.class);
                break;
            case R.id.ll_product_manage:
                //产品管理
                ActivitySkipUtil.skipAnotherActivity(activity, ProductManageActivity.class);
                break;
            case R.id.ll_business_consultant:
                //业务顾问
                ActivitySkipUtil.skipAnotherActivity(activity, MyBusinessConActivity.class);
                break;
        }
    }

    @Override
    public void showLoadingDialog(String s) {
        showProgress(s);
    }

    @Override
    public void closeLoadingDialog() {
        dismissProgress();
    }

    @Override
    public void onLoanIndexSuccess(int clickedPosition, LoanIndex loanIndex) {
        this.loanIndex=loanIndex;
        Context context=MineLoanActivity.this;
        if (loanIndex.getCity() != null) {
            if (StringUtil.isNotEmpty(loanIndex.getCity().toString())) {
                PreUtils.setCity(context, Integer.valueOf(new DecimalFormat("0").format(loanIndex.getCity())));
            }
        }

        //匹配表[实时抢单]角标
        int matchTabSub=loanIndex.getNewPPOrderCount();
        if(matchTabSub>0){
            if(matchTabSub>=99){
                tvSubGrabOrder.setText("99");
            }else{
                tvSubGrabOrder.setText(String.valueOf(matchTabSub));
            }
            tvSubGrabOrder.setVisibility(View.VISIBLE);
        }
        //产品申请表[专属订单]角标
        int productTabSub=loanIndex.getNewProductOrderCount();
        if(productTabSub>0){
            if(productTabSub>=99){
                tvSubExclusiveOrder.setText("99");
            }else{
                tvSubExclusiveOrder.setText(String.valueOf(productTabSub));
            }
        }
        int activation = loanIndex.getActivation();
        PreUtils.setCertificationStatus(context,activation);
        if(activation==1){
            //已激活
            PreUtils.setTrueName(context,loanIndex.getTrueName());
        }
        PreUtils.setCompanyID(context,String.valueOf(loanIndex.getCompanyID()));
        PreUtils.setCompanyTypeID(context,String.valueOf(loanIndex.getCompanyTypeID()));
    }

    /**
     * 去填写资料
     */
    private void showInfoDialog() {
        dialog = new CustomDialog(this, R.style.Customdialog, "卡贝贝提示", "请完善基本资料并认证"
                , new CustomDialog.ButtonListener() {
            @Override
            public void left() {
                dialog.dismiss();
            }

            @Override
            public void right() {
                ActivitySkipUtil.skipAnotherActivity(MineLoanActivity.this,
                        ModifyInfoActivity.class);
                dialog.dismiss();
            }
        }, getString(R.string.cancel), "设置基本资料");
        dialog.show();
    }

    /**
     * 去认证
     */
    private void showCertificationDialog() {
        dialog = new CustomDialog(this, R.style.Customdialog, "卡贝贝提示", "您未通过资质认证，请先资质认证"
                , new CustomDialog.ButtonListener() {
            @Override
            public void left() {
                dialog.dismiss();
            }

            @Override
            public void right() {
                dialog.dismiss();
                ActivitySkipUtil.skipAnotherActivity(MineLoanActivity.this,
                        InfoCertifyLoanActivity.class);
            }
        }, getString(R.string.cancel), "资质认证");
        dialog.show();
    }

    /**
     * 资料 审核中
     */
    private void showExamineDialog() {
        dialog = new CustomDialog(this, R.style.Customdialog, "卡贝贝提示", "您的资料审核中..."
                , new CustomDialog.ButtonListener() {
            @Override
            public void left() {
                dialog.dismiss();
            }

            @Override
            public void right() {
                dialog.dismiss();
            }
        }, getString(R.string.cancel), getString(R.string.determine));
        dialog.show();
    }

    /**
     * 提示认证不通过
     */
    private void showReasonDialog(String cancelActivationDescriptions) {
        dialog = new CustomDialog(this
                , R.style.Customdialog, "卡贝贝提示", "认证未通过(" + cancelActivationDescriptions + ")"
                , new CustomDialog.ButtonListener() {
            @Override
            public void left() {
                dialog.dismiss();
            }

            @Override
            public void right() {
                dialog.dismiss();
            }
        }, getString(R.string.cancel), getString(R.string.determine));
        dialog.show();
    }

    /**
     * 提示 去店铺
     */
    private void showStoreDialog() {
        dialog = new CustomDialog(this, R.style.Customdialog, "卡贝贝提示", "建议您先完善店铺信息,匹配客户更精准！"
                , new CustomDialog.ButtonListener() {
            @Override
            public void left() {
                dialog.dismiss();
            }

            @Override
            public void right() {
                ActivitySkipUtil.skipAnotherActivity(MineLoanActivity.this,
                        StoreSettingActivity.class);
                dialog.dismiss();
            }
        }, getString(R.string.cancel), "店铺设置");
        dialog.show();
    }

    //匹配申请表[实时抢单]
    public void toMatchTabActivity() {
        ActivitySkipUtil.skipAnotherActivity(this, MatchTableActivity.class);
    }

    //产品申请表[专属订单]
    public void toProductTabActivity() {
        ActivitySkipUtil.skipAnotherActivity(this, ProductTableActivity.class);
    }

    //已购买客户[我的订单]
    public void toBoughtTableActivity() {
        ActivitySkipUtil.skipAnotherActivity(this, BoughtTableActivity.class);
    }

    private void handleUserStatus(int clickedPosition){
        if(clickedPosition!=MineLoanPresenter.POSITION_NONE){
            switch (loanIndex.getActivation()) {
                case 2://待审
                case 3://不通过
                case 4://未填写
                case 0://未激活
                    switch (loanIndex.getGoToType()){
                        case "0"://不跳转
                            break;
                        case "1"://去填基本资料
                            showInfoDialog();
                            break;
                        case "2"://去认证
                            showCertificationDialog();
                            break;
                        case "3"://提示资料待审中
                            showExamineDialog();
                            break;
                        case "4"://认证未通过提示原因
                            showReasonDialog(loanIndex.getCancelActivationDescriptions());
                            break;
                        case "5"://去店铺
                            showStoreDialog();
                            break;
                    }
                    break;
                case 1://激活
                    switch (clickedPosition){
                        case MineLoanPresenter.POSITION_GRAB_ORDER:
                            //匹配申请表
                            toMatchTabActivity();
                            break;
                        case MineLoanPresenter.POSITION_EXCLUSIVE_ORDER:
                            //产品申请表
                            toProductTabActivity();
                            break;
                        case MineLoanPresenter.POSITION_MINE_ORDER:
                            //已购买客户
                            toBoughtTableActivity();
                            break;
                    }
                    break;
            }
        }
    }
}
